Skip to content

R50-T004: Test an invariant of swap (OddEvenSort.js) [Sorts/OddEvenSort.js] - #102

Open
stavansomaiya wants to merge 1 commit into
S-AI-Projects:masterfrom
stavansomaiya:student/e26b07f1064/R50-T004
Open

stavansomaiya wants to merge 1 commit into
S-AI-Projects:masterfrom
stavansomaiya:student/e26b07f1064/R50-T004

Conversation

@stavansomaiya

@stavansomaiya stavansomaiya commented Nov 23, 2026 •

Copy link
Copy Markdown

Description

Task R50-T004 · task 4 of 10 · Invariant/property checks
Issue #4 · Test an invariant of swap
Student Stavan Somaiya · URN e26b07f1064
File → function Sorts/OddEvenSort.js → swap (line 10 at the pinned commit, line 22 after this PR)
Pinned source TheAlgorithms/JavaScript @ 5c39e87 · OddEvenSort.js#L10
Branch stavansomaiya:student/e26b07f1064/R50-T004 → S-AI-Projects:master (one commit on 5c39e87)
Outcome The invariant holds on the fixture and on 100 seeded cases. The implementation is unchanged.

Summary

Adds invariant/property tests around swap in Sorts/OddEvenSort.js.

Why the invariant holds: swap only exchanges two positions, so the multiset of values is preserved. oddEvenSort moves values only through swap, and stops only when a full odd pass and a full even pass make no swap, i.e. no adjacent pair is out of order. So the output is (1) ordered, (2) a permutation of the input with the same multiplicities, and (3) the input array itself, sorted in place. The source defines an ascending interface only.

Files changed

File Lines What changed
Sorts/OddEvenSort.js +27 / −2 swap exported; JSDoc stating the invariant (no logic change)
Sorts/test/OddEvenSort.test.js +126 / −1 direct swap tests, 6 fixture tests, 5 property tests over 100 seeded cases
work/R50-T004/ (8 files) evidence check.py runner, README.md write-up, evidence.mjs, results.json and logs; coursework only, kept out of any upstream PR

Related Issue

Fixes: #4

This PR covers only R50-T004. Each of the 10 tasks has its own branch, commit and PR; see All 10 tasks under Additional Notes.

Type of Change

  • Bug Fix
  • New Feature
  • UI/UX Improvement
  • Performance Improvement
  • Tests
  • Documentation
  • Refactoring / Maintenance
  • CI/CD

Changes Made

  • Sorts/test/OddEvenSort.test.js: direct swap tests covering the in-place exchange, the undefined return and preserved multiplicities. The fixture covers [3,1,2], [2,2,1], [1,2,3], [3,2,1], [] and [7]. The property run has 100 cases from a fixed seed (mulberry32, 20261123, length 0..12, integers -5..5). Ordering, multiplicities and the in-place return are asserted separately. Literal generated cases are pinned, two readable small cases are kept, and any failure reports the shortest failing input.
  • Sorts/OddEvenSort.js: swap exported with JSDoc stating the invariant; JSDoc on oddEvenSort listing the three properties. No logic change.
  • work/R50-T004/: check.py (runs two deliberately broken temporary copies), evidence.mjs and results.

Screenshots / Recordings

There is no UI, so the recording is the terminal output of a fresh run on 23 Nov 2026. The run used a clean checkout of this branch with a new npm ci from the lockfile. The output is copied verbatim; only the temporary checkout path is replaced by a label.

Focused tests: npx vitest run … --reporter=verbose, 17/17 passed
$ npx vitest run Sorts/test/OddEvenSort.test.js --reporter=verbose
 RUN  v1.2.1 <clean checkout of student/e26b07f1064/R50-T004>
 ✓ Sorts/test/OddEvenSort.test.js > The OddEvenSort of the array [5, 4, 3, 2, 1] is [1, 2, 3, 4, 5]
 ✓ Sorts/test/OddEvenSort.test.js > The OddEvenSort of the array [] is []
 ✓ Sorts/test/OddEvenSort.test.js > The OddEvenSort of the array [10, 14, 12, 20] is [10, 12, 14, 20]
 ✓ Sorts/test/OddEvenSort.test.js > The OddEvenSort of the array [166, 169, 144] is [144, 166, 169]
 ✓ Sorts/test/OddEvenSort.test.js > swap invariant > swap exchanges two positions in place and returns undefined
 ✓ Sorts/test/OddEvenSort.test.js > swap invariant > swap preserves multiplicities
 ✓ Sorts/test/OddEvenSort.test.js > oddEvenSort fixture (ascending, in place) > oddEvenSort([3,1,2]) is [1,2,3] and returns the same array
 ✓ Sorts/test/OddEvenSort.test.js > oddEvenSort fixture (ascending, in place) > oddEvenSort([2,2,1]) is [1,2,2] and returns the same array
 ✓ Sorts/test/OddEvenSort.test.js > oddEvenSort fixture (ascending, in place) > oddEvenSort([1,2,3]) is [1,2,3] and returns the same array
 ✓ Sorts/test/OddEvenSort.test.js > oddEvenSort fixture (ascending, in place) > oddEvenSort([3,2,1]) is [1,2,3] and returns the same array
 ✓ Sorts/test/OddEvenSort.test.js > oddEvenSort fixture (ascending, in place) > oddEvenSort([]) is [] and returns the same array
 ✓ Sorts/test/OddEvenSort.test.js > oddEvenSort fixture (ascending, in place) > oddEvenSort([7]) is [7] and returns the same array
 ✓ Sorts/test/OddEvenSort.test.js > oddEvenSort properties over 100 seeded cases > the generator is deterministic for a fixed seed
 ✓ Sorts/test/OddEvenSort.test.js > oddEvenSort properties over 100 seeded cases > readable small generated cases sort as expected
 ✓ Sorts/test/OddEvenSort.test.js > oddEvenSort properties over 100 seeded cases > output is ordered
 ✓ Sorts/test/OddEvenSort.test.js > oddEvenSort properties over 100 seeded cases > output preserves input multiplicities
 ✓ Sorts/test/OddEvenSort.test.js > oddEvenSort properties over 100 seeded cases > output is the input array, mutated in place
 Test Files  1 passed (1)
      Tests  17 passed (17)
Automated check: python3 work/R50-T004/check.py, exit 0
$ python3 work/R50-T004/check.py
[PASS] oddEvenSort([3,1,2])
       input:    [3, 1, 2]
       expected: {"result": [1, 2, 3], "returnsSameArray": true}
       actual:   {"result": [1, 2, 3], "returnsSameArray": true}
       source:   OddEvenSort.js:41 ascending, in place, returns arr (line 58)
[PASS] oddEvenSort([2,2,1])
       input:    [2, 2, 1]
       expected: {"result": [1, 2, 2], "returnsSameArray": true}
       actual:   {"result": [1, 2, 2], "returnsSameArray": true}
       source:   OddEvenSort.js:41 ascending, in place, returns arr (line 58)
[PASS] oddEvenSort([1,2,3])
       input:    [1, 2, 3]
       expected: {"result": [1, 2, 3], "returnsSameArray": true}
       actual:   {"result": [1, 2, 3], "returnsSameArray": true}
       source:   OddEvenSort.js:41 ascending, in place, returns arr (line 58)
[PASS] oddEvenSort([3,2,1])
       input:    [3, 2, 1]
       expected: {"result": [1, 2, 3], "returnsSameArray": true}
       actual:   {"result": [1, 2, 3], "returnsSameArray": true}
       source:   OddEvenSort.js:41 ascending, in place, returns arr (line 58)
[PASS] oddEvenSort([])
       input:    []
       expected: {"result": [], "returnsSameArray": true}
       actual:   {"result": [], "returnsSameArray": true}
       source:   OddEvenSort.js:41 ascending, in place, returns arr (line 58)
[PASS] oddEvenSort([7])
       input:    [7]
       expected: {"result": [7], "returnsSameArray": true}
       actual:   {"result": [7], "returnsSameArray": true}
       source:   OddEvenSort.js:41 ascending, in place, returns arr (line 58)
[PASS] swap([3, 1, 2], 0, 1)
       input:    [[3, 1, 2], 0, 1]
       expected: {"arr": [1, 3, 2], "returned": "undefined"}
       actual:   {"arr": [1, 3, 2], "returned": "undefined"}
       source:   OddEvenSort.js:22-26 swap mutates arr, returns nothing
[PASS] property run: 100 cases, seed 20261123, length 0..12, values -5..5
       input:    {"seed": 20261123, "cases": 100}
       expected: {"ordered": 100, "multiplicitiesPreserved": 100}
       actual:   {"ordered": 100, "multiplicitiesPreserved": 100}
       source:   OddEvenSort.js:46,52 numbers compared with >
[KILLED] mutant swap loses a value (arr[j] = arr[i]): '  arr[j] = tmp\n' -> '  arr[j] = arr[i]\n'
       failed:  The OddEvenSort of the array [5, 4, 3, 2, 1] is [1, 2, 3, 4, 5]
       failed:  The OddEvenSort of the array [10, 14, 12, 20] is [10, 12, 14, 20]
       failed:  The OddEvenSort of the array [166, 169, 144] is [144, 166, 169]
       failed:  swap invariant swap exchanges two positions in place and returns undefined
       failed:  swap invariant swap preserves multiplicities
       failed:  oddEvenSort fixture (ascending, in place) oddEvenSort([3,1,2]) is [1,2,3] and returns the same array
       failed:  oddEvenSort fixture (ascending, in place) oddEvenSort([2,2,1]) is [1,2,2] and returns the same array
       failed:  oddEvenSort fixture (ascending, in place) oddEvenSort([3,2,1]) is [1,2,3] and returns the same array
       failed:  oddEvenSort properties over 100 seeded cases readable small generated cases sort as expected
       failed:  oddEvenSort properties over 100 seeded cases output preserves input multiplicities
[KILLED] mutant even pass no longer clears the sorted flag: '    for (let i = 0; i < arr.length - 1; i += 2) {\n      if (arr[i] > arr[i + 1]) {\n        swap(arr, i, i + 1)\n        sorted = false\n' -> '    for (let i = 0; i < arr.length - 1; i += 2) {\n      if (arr[i] > arr[i + 1]) {\n        swap(arr, i, i + 1)\n'
       failed:  oddEvenSort fixture (ascending, in place) oddEvenSort([3,1,2]) is [1,2,3] and returns the same array
       failed:  oddEvenSort properties over 100 seeded cases output is ordered

focused tests: 17/17 passed  (node_modules/.bin/vitest run Sorts/test/OddEvenSort.test.js --reporter=json --outputFile=work/R50-T004/vitest-focused.raw.json)
npm test:      11983/11983 passed  (npm test -- --reporter=json --outputFile=work/R50-T004/vitest-full.raw.json)

R50-T004: all checks passed
(exit 0)

Testing

Automated check: python3 work/R50-T004/check.py installs from the lockfile and runs the focused tests. It then prints input / expected / actual for every fixture row, runs each deliberate mutant in a temporary copy, and finally runs the repository test script in non-watch mode. It exits non-zero if any assertion or step fails, and records every command in commands.log.

To reproduce:

git fetch https://github.com/stavansomaiya/TheAlgorithms-JavaScript.git student/e26b07f1064/R50-T004
git checkout FETCH_HEAD
python3 work/R50-T004/check.py

Recorded results (committed in results.json; tool versions in versions.txt):

Check Command Result
Install from lockfile npm ci --ignore-scripts --no-audit --no-fund ✅ exit 0
Focused tests vitest run Sorts/test/OddEvenSort.test.js ✅ 17/17 passed
Fixture evidence (input / expected / actual / source line) node work/R50-T004/evidence.mjs ✅ 8/8 rows match
Mutant (temporary copy): swap loses a value (arr[j] = arr[i]) applied by check.py ✅ caught by output preserves input multiplicities
Mutant (temporary copy): even pass no longer clears the sorted flag applied by check.py ✅ caught by output is ordered
Repository test script, non-watch npm test (vitest run) ✅ 11983/11983 passed
Code style npm run check-style (Prettier) ✅ passes
Spelling (CI settings) codespell ✅ no new findings

Acceptance criteria → evidence:

# Done when (quoted from #4) Evidence in this PR
1 The checked behavior is tied to swap in Sorts/OddEvenSort.js; every assertion has an explicit expected value. ✅ swap exchanges two positions in place and returns undefined: swap([3, 1, 2], 0, 1) leaves [1, 3, 2] and returns undefined. The fixture tests compare with literal arrays, and the generator test pins literal generated cases (case 0 is [4, 0, -1, -2, 0, -1, -5, 1, -4, 0, -3]).
2 Use [3, 1, 2], [2, 2, 1], already ordered [1, 2, 3], reversed [3, 2, 1], and the smallest supported collection. Expected ascending outputs are [1, 2, 3] and [1, 2, 2]; honor a descending interface if that is what the source defines. ✅ One test per fixture input: [3,1,2] → [1,2,3], [2,2,1] → [1,2,2], [1,2,3] → [1,2,3], [3,2,1] → [1,2,3], smallest [] → [], plus [7] → [7]. Each also asserts that the same array is returned. The source defines ascending order only.
3 Existing supported behavior and the public interface are preserved unless a failing regression demonstrates and documents a defect. ✅ No logic change (swap exported, JSDoc added). The 4 existing tests pass, and the full suite passes (11983/11983).
4 The generator never relies on undefined input behavior; the invariant is justified in the PR. ✅ The generator produces only finite integers −5..5 with lengths 0..12: the comparable-number domain of the > comparisons at lines 46 and 52. No NaN, mixed types or holes. Seed 20261123 (mulberry32). The invariant is justified in the Description above.
5 The checks fail when the relevant invariant is deliberately broken in a temporary copy. ✅ check.py breaks the invariant in two temporary copies. With arr[j] = arr[i] in swap, output preserves input multiplicities fails ([2, 1] → [1, 1]). With sorted = false dropped from the even pass, output is ordered fails ([3, 1, 2] → [1, 3, 2]).

Evidence files: check.py · README.md (contract, fixture, baseline, findings) · evidence.mjs · results.json · vitest-focused.json

Checklist

  • I have tested my changes locally.
  • I have added or updated tests, if needed.
  • I have updated documentation, if needed.
  • I have followed the project's coding guidelines.
  • I have run the relevant tests and checks.
  • I have addressed previous review feedback, if applicable. (No review feedback yet.)

Additional Notes

The checks fail when the invariant is deliberately broken in a temporary copy. With arr[j] = arr[i] in swap, output preserves input multiplicities fails (smallest case [2, 1] → [1, 1]). Dropping sorted = false in the even pass makes output is ordered fail ([3, 1, 2] → [1, 3, 2]). The generator only produces finite integers, inside the comparable-number domain of the > comparisons, so it never relies on undefined input behaviour.

Scope: the PR changes only Sorts/OddEvenSort.js, its test file and work/R50-T004/. work/R50-T004/ is coursework evidence and would be removed from any later upstream PR.

All 10 tasks (one PR each)
Task Issue Branch Pull request
R50-T001: Lock down the contract of setBit #1 R50-T001 #99
R50-T002: Define the supported boundaries of checkKeywordValidity #2 R50-T002 #100
R50-T003: Cross-check getParentPosition #3 R50-T003 #101
R50-T004: Test an invariant of swap #4 R50-T004 #102 (this PR)
R50-T005: Make verification repeatable for newGeneration #5 R50-T005 #103
R50-T006: Simplify one responsibility in countLetters #6 R50-T006 #104
R50-T007: Publish a runnable example for DisjointSetTreeNode #7 R50-T007 #105
R50-T008: Cover the failure behavior of TrieNode #8 R50-T008 #106
R50-T009: Measure the work done by fibonacci #9 R50-T009 #107
R50-T010: Strengthen the tests of OpenKnightTour #10 R50-T010 #108

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stavan Somaiya — R50-T004: Test an invariant of swap (OddEvenSort.js) [Sorts/OddEvenSort.js]

1 participant